package paixu;

import java.util.Arrays;

/**
 * @author lj
 * @Description: 插入排序
 * @date 2020-10-14 13:41
 * @QQ 851477108
 */


public class chaR {
    public static void main(String[] args) {
        int[] arr = {5,8,6,2,4,3};

        int[] sort = sort(arr);
        for (int value : sort) {
            System.out.print(value+",");
        }
        System.out.println(Arrays.toString(arr));

    }

    public static int[] sort(int[] arr) {
        //如果传入的数组为空或者只有一个值,就直接返回
        if (arr == null||arr.length < 2){
            return arr;
        }
        //不为空则进循环判断
        for (int i = 1; i < arr.length; i++) {
            for (int j = i - 1; j >= 0; j--){
                if (arr[j]>arr[j+1]){
                    swap(arr,j,j+1);
                }
            }
        }
        return arr;
    }

    public static void swap(int[] arr, int j, int i) {
        arr[i] = arr[i] ^ arr[j];
        arr[j] = arr[i] ^ arr[j];
        arr[i] = arr[i] ^ arr[j];
    }
}
